using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Bll;
using Entities;

public partial class shoppingcart : MyPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Check existance shoppingcart
            if (HttpContext.Current.Session["ShoppingCart"] != null)
            {
                Bll.CartBll cart = (Bll.CartBll)HttpContext.Current.Session["ShoppingCart"];
                if (cart.Count > 0)
                {
                    //Check existance dealer role for caculating discount -- will be upgrade with customer role + discout later
                    if (role.Trim() == "dealer")
                    {
                        AccountBll accBll = new AccountBll();
                        AccountInfo accInfo = accBll.GetByUserName(username);
                        float subtotal = (float)cart.Total;
                        float percent = accInfo.Discount;
                        float percentSubtotal = (subtotal * accInfo.Discount) / 100;
                        float finalSubtotal = subtotal - percentSubtotal;
                        //Save the prize after discount into a session
                        Session["Subtotal"] = (decimal)finalSubtotal;
                        gvCart.DataSource = cart.GetOrderLineItems();
                        gvCart.DataBind();
                        lblSubTotal1.Text = "<b>Subtotal : </b>" + "<b style='color:red'>" + finalSubtotal.ToString("C") + "</b>" + " (VAT included)";
                        lblSubtotal.Text = subtotal.ToString("C");
                        lblDiscount.Text = accInfo.Discount.ToString() + "%";
                        lblPercent.Text = "- " + percentSubtotal.ToString("C");
                        lblFinalSubtotal.Text = finalSubtotal.ToString("C");
                    }                    
                    else
                    {
                        gvCart.DataSource = cart.GetOrderLineItems();
                        gvCart.DataBind();
                        NoneDealerCart(cart.Total);
                        Session["Subtotal"] = cart.Total;
                    }
                }
                else
                {
                    HideCart();
                }
            }
            else
            {
                HideCart();
            }
        }
    }

    private void HideCart()
    {
        lblSubTotal1.Visible = false;
        ibtnCheckOut.Visible = false;
        panelCart.Visible = false;
        lblMessage.Text = "Your cart is empty !" + " Please click <a href='product.aspx'>here</a> to return our Product page.<br /><br />";
    }

    private void NoneDealerCart(decimal subtotal)
    {
        float mysubtotal = (float)subtotal;
        lblUSD.Text = "Prices based upon exchange rates as of 5/20/2009 and 9:00PM EST. Your order will be charged in US Dollars." +
                        " Due to currency changes the prices may not reflect the exact exchange rate at the time of transaction";
        lblSubTotal1.Text = "<b>Subtotal(USD) : </b>" + "<b style='color:red'>" + mysubtotal.ToString("C") + "</b>" + " (VAT included)";
        lblSubtotal.Text = mysubtotal.ToString("C");
        panelDiscount.Visible = false;
    }

    protected void gvCart_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Remove")
        {
            Bll.CartBll cart = (Bll.CartBll)HttpContext.Current.Session["ShoppingCart"];
            if (!string.IsNullOrEmpty(e.CommandArgument.ToString()))
            {
                cart.Remove(e.CommandArgument.ToString());
                HttpContext.Current.Session["ShoppingCart"] = cart;
                Response.Redirect("shoppingcart.aspx");
            }
        }
    }

    protected void ibtnUpdate_Click(object sender, ImageClickEventArgs e)
    {
        Bll.CartBll cart = (Bll.CartBll)HttpContext.Current.Session["ShoppingCart"];
        LineItemInfo[] lines = cart.GetOrderLineItems();
        for (int i = 0; i < lines.Length; i++)
        {
            TextBox txt = (TextBox)gvCart.Rows[lines[i].Line - 1].FindControl("txtQuantity");
            string quantity = txt.Text;
            try
            {
                int please = int.Parse(quantity);
                if (int.Parse(quantity) <= lines[i].UnitInStock)
                {
                    string itemID = lines[i].ItemId;
                    cart.SetQuantity(itemID, int.Parse(txt.Text));
                }
                else
                {
                    break;
                } 
            }
            catch (System.FormatException fe)
            {
                if(fe.Message != null)
                    break;
            }
                 
        }
        HttpContext.Current.Session["ShoppingCart"] = cart;
        Response.Redirect("shoppingcart.aspx");
    }

    protected void ibtnContinueShopping_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("product.aspx");
    }

    protected void ibtnCheckOut_Click(object sender, ImageClickEventArgs e)
    {
        ButtonCheckOutClick();
    }
    protected void ibtnCheckOutTop_Click(object sender, ImageClickEventArgs e)
    {
        ButtonCheckOutClick();
    }

    private void ButtonCheckOutClick()
    { 
        //1-Check Existance Username
        ArrayList arr = (ArrayList) Session["Login"];
        if (arr != null)
        {
            Response.Redirect("billing.aspx");                     
        }
        else
        {
            Response.Redirect("checkouterror.aspx");
        }
    }
}
